#!/bin/bash
#
# chkconfig: 2345 10 90
# description: Start and Stop the Tomcat daemon.
# Date  :08/13/2014
# Modify :change pidof to lsof
#        +Add permisson check

system=$2
javaHome=$3
tomcatHome=$4
httpPort=$5

export JAVA_HOME=${javaHome}
export PATH=$PATH:${javaHome}/bin
export CATALINA_HOME=${tomcatHome}
export CATALINA_BASE=${tomcatHome}
export HTTP_PORT=${httpPort}

#export start_ok=`echo -e "\033[32;40;25m [OK] \033[0m"`
#export stop_ok=`echo -e "\033[32;40;25m [OK] \033[0m"`

# Source function library. centos
if [ ${system} = "centos" ]
then
. /etc/rc.d/init.d/functions
fi

# ubuntu
if [ ${system} = "ubuntu" ]
then
. /lib/lsb/init-functions
fi


# check for tomcat script
if [ ! -f $CATALINA_HOME/bin/catalina.sh ]
then echo "catalina.sh not valilable..."
echo -e "\nUsage: tomcat.sh {start|stop|restart|status|check}"
exit
fi

#[ $(id -u) -ne 0 ] && echo "Please change to root user" && exit 1

function check_permisson(){
         #illegal_777_perm=(`find "$CATALINA_BASE" -perm 777 `)
         illegal_user_perm=(`find "$CATALINA_BASE" ! -user beetle -o ! -group beetle`)
         tomcat_pid=`netstat -tlnp 2>/dev/null |grep "${HTTP_PORT}"|awk '{print $7}'|awk -F '/' '{print $1}'`
         #if [ "${#illegal_777_perm[*]}" -ne 0 ] || [ ${#illegal_user_perm[*]} -ne 0 ];then
         if [ ${#illegal_user_perm[*]} -ne 0 ];then
            echo -e "Some dirs or files permisson are uncorrect!please modify it:"
           # echo "${illegal_777_perm[*]}" |xargs -n1
            echo "${illegal_user_perm[*]}" |xargs -n1
            return 1
         fi
         if [[ -n ${tomcat_pid} ]];then
            tomcat_user=`ps -ef |grep ${tomcat_pid} |grep -v grep |awk '{print $1}'`
               if [ "${tomcat_user}" != "beetle" ];then
                  echo "tomcat daemon  user is not beetle!"
                  return 2
               fi
         fi
#        echo "tomcat permisson check ----> ${start_ok}"
}

function start(){
        check_permisson
        if [ "$?" -ne 0 ];then exit 1;fi
        tomcat_pid=`netstat -tlnp 2>/dev/null |grep "${HTTP_PORT}"|awk '{print $7}'|awk -F '/' '{print $1}'`
        if [ "${tomcat_pid}" ] ;then
        echo "tomcat service is running"
        else
        eval "rm -rf $CATALINA_HOME/work/Catalina/localhost/* &>/dev/null"
        eval "sh $CATALINA_HOME/bin/startup.sh &>/dev/null"
        echo "Start Tomcat complete ok"
        fi
        }
function stop(){
        tomcat_pid=`netstat -tlnp 2>/dev/null |grep "${HTTP_PORT}"|awk '{print $7}'|awk -F '/' '{print $1}'`
        if [  "${tomcat_pid}" ];then
        eval "kill -9 ${tomcat_pid}"
        echo "Stop Tomcat complete ok"
        else
        echo "tomcat service is not running!"
        fi
        }
function restart(){
       stop
       sleep 2
       start
       }
function status(){
        tomcat_pid=`netstat -tlnp 2>/dev/null |grep "${HTTP_PORT}"|awk '{print $7}'|awk -F '/' '{print $1}'`
        if [ "${tomcat_pid}" ] ;then
        echo "tomcat service is running :${tomcat_pid}"
        else
        echo "tomcat service is not running"
        fi
}


case "$1" in
    start)
        start ;;
    stop)
        stop ;;
    restart)
        restart ;;
    status)
        status ;;
    check)
        check_permisson ;;
    *)
    echo "Usage: tomcat.sh {start|stop|restart|status|check}"
    exit 1
esac
exit 0
